home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Utilities / Type1Manager / src / type1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-12  |  57.2 KB  |  1,982 lines

  1. /* $XConsortium: type1.c,v 1.7 94/02/07 15:30:22 gildea Exp $ */
  2. /* Copyright International Business Machines, Corp. 1991
  3.  * All Rights Reserved
  4.  * Copyright Lexmark International, Inc. 1991
  5.  * All Rights Reserved
  6.  * Portions Copyright (c) 1990 Adobe Systems Incorporated.
  7.  * All Rights Reserved
  8.  *
  9.  * License to use, copy, modify, and distribute this software and its
  10.  * documentation for any purpose and without fee is hereby granted,
  11.  * provided that the above copyright notice appear in all copies and that
  12.  * both that copyright notice and this permission notice appear in
  13.  * supporting documentation, and that the name of IBM or Lexmark or Adobe
  14.  * not be used in advertising or publicity pertaining to distribution of
  15.  * the software without specific, written prior permission.
  16.  *
  17.  * IBM, LEXMARK, AND ADOBE PROVIDE THIS SOFTWARE "AS IS", WITHOUT ANY
  18.  * WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT
  19.  * LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  20.  * PARTICULAR PURPOSE, AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.  THE
  21.  * ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE, INCLUDING
  22.  * ANY DUTY TO SUPPORT OR MAINTAIN, BELONGS TO THE LICENSEE.  SHOULD ANY
  23.  * PORTION OF THE SOFTWARE PROVE DEFECTIVE, THE LICENSEE (NOT IBM,
  24.  * LEXMARK, OR ADOBE) ASSUMES THE ENTIRE COST OF ALL SERVICING, REPAIR AND
  25.  * CORRECTION.  IN NO EVENT SHALL IBM, LEXMARK, OR ADOBE BE LIABLE FOR ANY
  26.  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  27.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  28.  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  29.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  30.  */
  31.  
  32. /*********************************************************************/
  33. /*                                                                   */
  34. /* Type 1 module - Converting fonts in Adobe Type 1 Font Format      */
  35. /*                 to scaled and hinted paths for rasterization.     */
  36. /*                 Files: type1.c, type1.h, and blues.h.             */
  37. /*                                                                   */
  38. /* Authors:   Sten F. Andler, IBM Almaden Research Center            */
  39. /*                 (Type 1 interpreter, stem & flex hints)           */
  40. /*                                                                   */
  41. /*            Patrick A. Casey, Lexmark International, Inc.          */
  42. /*                 (Font level hints & stem hints)                   */
  43. /*                                                                   */
  44. /*********************************************************************/
  45.  
  46. /******************/
  47. /* Include Files: */
  48. /******************/
  49. #ifndef T1GST
  50. #include "global.h"
  51. #endif
  52.  
  53.  
  54. /**********************************/
  55. /* Type1 Constants and Structures */
  56. /**********************************/
  57. #define MAXSTACK 24        /* Adobe Type1 limit */
  58. #define MAXCALLSTACK 10        /* Adobe Type1 limit */
  59. #define MAXPSFAKESTACK 32    /* Max depth of fake PostScript stack (local) */
  60. #define MAXSTRLEN 512        /* Max length of a Type 1 string (local) */
  61. //#define MAXLABEL 256        /* Maximum number of new hints */
  62. #define MAXSTEMS 128        /* Maximum number of VSTEM and HSTEM hints */
  63. #define EPS 0.001        /* Small number for comparisons */
  64.  
  65.  
  66. /************************************/
  67. /* Adobe Type 1 CharString commands */
  68. /************************************/
  69. #define HSTEM        1
  70. #define VSTEM        3
  71. #define VMOVETO      4
  72. #define RLINETO      5
  73. #define HLINETO      6
  74. #define VLINETO      7
  75. #define RRCURVETO    8
  76. #define CLOSEPATH    9
  77. #define CALLSUBR    10
  78. #define RETURN      11
  79. #define ESCAPE      12
  80. #define HSBW        13
  81. #define ENDCHAR     14
  82. #define RMOVETO     21
  83. #define HMOVETO     22
  84. #define VHCURVETO   30
  85. #define HVCURVETO   31
  86.  
  87.  
  88. /*******************************************/
  89. /* Adobe Type 1 CharString Escape commands */
  90. /*******************************************/
  91. #define DOTSECTION       0
  92. #define VSTEM3           1
  93. #define HSTEM3           2
  94. #define SEAC             6
  95. #define SBW              7
  96. #define DIV             12
  97. #define CALLOTHERSUBR   16
  98. #define POP             17
  99. #define SETCURRENTPOINT 33
  100.  
  101.  
  102. /*****************/
  103. /* Useful macros */
  104. /*****************/
  105. static double tmpx;        /* Store macro argument in tmpx to avoid re-evaluation */
  106. static long tmpi;        /* Store converted value in tmpi to avoid re-evaluation */
  107.  
  108. #define FABS(x) (((tmpx = (x)) < 0.0) ? -tmpx : tmpx)
  109.  
  110. #define CEIL(x) (((tmpi = (long) (tmpx = (x))) < tmpx) ? ++tmpi : tmpi)
  111.  
  112. #define FLOOR(x) (((tmpi = (long) (tmpx = (x))) > tmpx) ? --tmpi : tmpi)
  113.  
  114. #define ROUND(x) FLOOR((x) + 0.5)
  115.  
  116. #define ODD(x) (((int)(x)) & 01)
  117.  
  118. #define Error {errflag = TRUE; return;}
  119. #define ErrorRet(ret) {errflag = TRUE; return (ret);}
  120.  
  121. #define Error0(errmsg) {IfTrace0(TRUE, errmsg); Error;}
  122. #define Error0Ret(errmsg, ret) {IfTrace0(TRUE, errmsg); ErrorRet(ret);}
  123.  
  124. #define Error1(errmsg,arg) {IfTrace1(TRUE, errmsg, arg); Error;}
  125.  
  126.  
  127. /********************/
  128. /* global variables */
  129. /********************/
  130. struct stem
  131. {                                        /* representation of a STEM hint */
  132.     int vertical;                        /* TRUE if vertical, FALSE otherwise */
  133.     double x, dx;                        /* interval of vertical stem */
  134.     double y, dy;                        /* interval of horizontal stem */
  135.     struct segment *lbhint, *lbrevhint;    /* left  or bottom hint adjustment */
  136.     struct segment *rthint, *rtrevhint;    /* right or top    hint adjustment */
  137. };
  138.  
  139. extern struct XYspace *IDENTITY;
  140.  
  141. static double escapementX, escapementY;
  142. static double sidebearingX, sidebearingY;
  143. static double accentoffsetX, accentoffsetY;
  144.  
  145. static struct segment *path;
  146. static int errflag;
  147.  
  148.  
  149. /*************************************************/
  150. /* Global variables to hold Type1Char parameters */
  151. /*************************************************/
  152. static psfont *Environment;
  153. static struct XYspace *CharSpace;
  154. static psobj *CharStringP, *SubrsP, *OtherSubrsP;
  155. static int *ModeP;
  156.  
  157.  
  158. /************************/
  159. /* Forward declarations */
  160. /************************/
  161. static void ComputeAlignmentZones(void);
  162. static void InitStems(void);
  163. static void FinitStems(void);
  164. static void ComputeStem(int stemno);
  165. static struct segment *Applyhint(struct segment *p, int stemnumber, int half);
  166. static struct segment *Applyrevhint(struct segment *p, int stemnumber, int half);
  167. static struct segment *FindStems(double x, double y, double dx, double dy);
  168. static void ClearStack(void);
  169. static void Push(double Num);
  170. static void ClearCallStack(void);
  171. static void PushCall(psobj *CurrStrP, int CurrIndex, unsigned short CurrKey);
  172. static void PopCall(psobj **CurrStrPP, int *CurrIndexP, unsigned short *CurrKeyP);
  173. static void ClearPSFakeStack(void);
  174. static void PSFakePush(double Num);
  175. static double PSFakePop(void);
  176. static struct segment *CenterStem(double edge1, double edge2);
  177. static unsigned char Decrypt(unsigned char cipher);
  178. static int DoRead(int *CodeP);
  179. static void StartDecrypt(void);
  180. static void Decode(int Code);
  181. static void DoCommand(int Code);
  182. static void Escape(int Code);
  183. static void HStem(double y, double dy);
  184. static void VStem(double x, double dx);
  185. static void RLineTo(double dx, double dy);
  186. static void RRCurveTo(double dx1, double dy1, double dx2, double dy2, double dx3, double dy3);
  187. static void DoClosePath(void);
  188. static void CallSubr(int subrno);
  189. static void Return(void);
  190. static void EndChar(void);
  191. static void RMoveTo(double dx, double dy);
  192. static void DotSection(void);
  193. static void Seac(double asb, double adx, double ady, unsigned char bchar, unsigned char achar);
  194. static void Sbw(double sbx, double sby, double wx, double wy);
  195. static double Div(double num1, double num2);
  196. static void FlxProc( double c1x2, double c1y2, double c3x0, double c3y0, double c3x1, double c3y1, double c3x2, double c3y2, double c4x0, double c4y0, double c4x1, double c4y1, double c4x2, double c4y2, double epY, double epX, int idmin);
  197. static void FlxProc1(void);
  198. static void FlxProc2(void);
  199. static void HintReplace(void);
  200. static void CallOtherSubr(int othersubrno);
  201. static void SetCurrentPoint(double x, double y);
  202.  
  203.  
  204. /*****************************************/
  205. /* statics for Flex procedures (FlxProc) */
  206. /*****************************************/
  207. static struct segment *FlxOldPath;    /* save path before Flex feature */
  208.  
  209.  
  210. /******************************************************/
  211. /* statics for Font level hints (Blues) (see blues.h) */
  212. /******************************************************/
  213. static struct blues_struct *blues;    /* the blues structure */
  214. static struct alignmentzone alignmentzones[MAXALIGNMENTZONES];
  215. static int numalignmentzones;        /* total number of alignment zones */
  216.  
  217.  
  218. /****************************************************************/
  219. /* Subroutines for the Font level hints (Alignment zones, etc.) */
  220. /****************************************************************/
  221.  
  222.  
  223. /******************************************/
  224. /* Fill in the alignment zone structures. */
  225. /******************************************/
  226. static void ComputeAlignmentZones(void)
  227. {
  228.     int i;
  229.     double dummy, bluezonepixels, familyzonepixels;
  230.     struct segment *p;
  231.  
  232.     numalignmentzones = 0;    /* initialize total # of zones */
  233.  
  234.     /* do the BlueValues zones */
  235.     for (i = 0; i < blues->numBlueValues; i += 2, ++numalignmentzones)
  236.     {
  237.         /* the 0th & 1st numbers in BlueValues are for a bottom zone */
  238.         /* the rest are topzones */
  239.         if (i == 0)    /* bottom zone */
  240.             alignmentzones[numalignmentzones].topzone = FALSE;
  241.         else        /* top zone */
  242.             alignmentzones[numalignmentzones].topzone = TRUE;
  243.         if (i < blues->numFamilyBlues)
  244.         {        /* we must consider FamilyBlues */
  245.             p = ILoc(CharSpace, 0, blues->BlueValues[i] - blues->BlueValues[i + 1]);
  246.             QueryLoc(p, IDENTITY, &dummy, &bluezonepixels);
  247.             Destroy(p);
  248.             p = ILoc(CharSpace, 0, blues->FamilyBlues[i] - blues->FamilyBlues[i + 1]);
  249.             QueryLoc(p, IDENTITY, &dummy, &familyzonepixels);
  250.             Destroy(p);
  251.             /* is the difference in size of the zones less than 1 pixel? */
  252.             if (FABS(bluezonepixels - familyzonepixels) < 1.0)
  253.             {
  254.                 /* use the Family zones */
  255.                 alignmentzones[numalignmentzones].bottomy =
  256.                     blues->FamilyBlues[i];
  257.                 alignmentzones[numalignmentzones].topy =
  258.                     blues->FamilyBlues[i + 1];
  259.                 continue;
  260.             }
  261.         }
  262.         /* use this font's Blue zones */
  263.         alignmentzones[numalignmentzones].bottomy = blues->BlueValues[i];
  264.         alignmentzones[numalignmentzones].topy = blues->BlueValues[i + 1];
  265.     }
  266.  
  267.     /* do the OtherBlues zones */
  268.     for (i = 0; i < blues->numOtherBlues; i += 2, ++numalignmentzones)
  269.     {
  270.         /* all of the OtherBlues zones are bottom zones */
  271.         alignmentzones[numalignmentzones].topzone = FALSE;
  272.         if (i < blues->numFamilyOtherBlues)
  273.         {        /* consider FamilyOtherBlues  */
  274.             p = ILoc(CharSpace, 0, blues->OtherBlues[i] - blues->OtherBlues[i + 1]);
  275.             QueryLoc(p, IDENTITY, &dummy, &bluezonepixels);
  276.             Destroy(p);
  277.             p = ILoc(CharSpace, 0, blues->FamilyOtherBlues[i] -
  278.                  blues->FamilyOtherBlues[i + 1]);
  279.             QueryLoc(p, IDENTITY, &dummy, &familyzonepixels);
  280.             Destroy(p);
  281.             /* is the difference in size of the zones less than 1 pixel? */
  282.             if (FABS(bluezonepixels - familyzonepixels) < 1.0)
  283.             {
  284.                 /* use the Family zones */
  285.                 alignmentzones[numalignmentzones].bottomy =
  286.                     blues->FamilyOtherBlues[i];
  287.                 alignmentzones[numalignmentzones].topy =
  288.                     blues->FamilyOtherBlues[i + 1];
  289.                 continue;
  290.             }
  291.         }
  292.         /* use this font's Blue zones (as opposed to the Family Blues */
  293.         alignmentzones[numalignmentzones].bottomy = blues->OtherBlues[i];
  294.         alignmentzones[numalignmentzones].topy = blues->OtherBlues[i + 1];
  295.     }
  296. }
  297.  
  298.  
  299. /**********************************************************************/
  300. /* Subroutines and statics for handling of the VSTEM and HSTEM hints. */
  301. /**********************************************************************/
  302. static int InDotSection;                /* DotSection flag */
  303. static struct stem stems[MAXSTEMS];    /* All STEM hints */
  304. static int numstems;                    /* Number of STEM hints */
  305. static int currstartstem;                /* The current starting stem. */
  306. static int oldvert, oldhor;            /* Remember hint in effect */
  307. static int oldhorhalf, oldverthalf;    /* Remember which half of the stem */
  308. static double wsoffsetX, wsoffsetY;    /* White space offset - for VSTEM3,HSTEM3 */
  309. static int wsset;                        /* Flag for whether we've set wsoffsetX,Y */
  310.  
  311. static void InitStems(void)        /* Initialize the STEM hint data structures */
  312. {
  313.     InDotSection = FALSE;
  314.     currstartstem = numstems = 0;
  315.     oldvert = oldhor = -1;
  316. }
  317.  
  318.  
  319. static void FinitStems(void)        /* Terminate the STEM hint data structures */
  320. {
  321.     int i;
  322.  
  323.     for (i = 0; i < numstems; i++)
  324.     {
  325.         Destroy(stems[i].lbhint);
  326.         Destroy(stems[i].lbrevhint);
  327.         Destroy(stems[i].rthint);
  328.         Destroy(stems[i].rtrevhint);
  329.     }
  330. }
  331.  
  332.  
  333. /*******************************************************************/
  334. /* Compute the dislocation that a stemhint should cause for points */
  335. /* inside the stem.                                                */
  336. /*******************************************************************/
  337. static void ComputeStem(int stemno)
  338. {
  339.     int verticalondevice, idealwidth;
  340.     double stemstart, stemwidth;
  341.     struct segment *p;
  342.     int i;
  343.     double stembottom, stemtop, flatposition;
  344.     double Xpixels, Ypixels;
  345.     double unitpixels, onepixel;
  346.     int suppressovershoot, enforceovershoot;
  347.     double stemshift, flatpospixels, overshoot;
  348.     double widthdiff;    /* Number of character space units to adjust width */
  349.     double lbhintvalue, rthintvalue;
  350.     double cxx, cyx, cxy, cyy;    /* Transformation matrix */
  351.     int rotated;        /* TRUE if character is on the side, FALSE if upright */
  352.  
  353.     /************************************************/
  354.     /* DETERMINE ORIENTATION OF CHARACTER ON DEVICE */
  355.     /************************************************/
  356.  
  357.     QuerySpace(CharSpace, &cxx, &cyx, &cxy, &cyy);    /* Transformation matrix */
  358.  
  359.     if (FABS(cxx) < 0.00001 || FABS(cyy) < 0.00001)
  360.         rotated = TRUE;    /* Char is on side (90 or 270 degrees), possibly oblique. */
  361.     else if (FABS(cyx) < 0.00001 || FABS(cxy) < 0.00001)
  362.         rotated = FALSE;/* Char is upright (0 or 180 degrees), possibly oblique. */
  363.     else
  364.     {
  365.         stems[stemno].lbhint = NULL;    /* Char is at non-axial angle, ignore hints. */
  366.         stems[stemno].lbrevhint = NULL;
  367.         stems[stemno].rthint = NULL;
  368.         stems[stemno].rtrevhint = NULL;
  369.         return;
  370.     }
  371.  
  372.     /* Determine orientation of stem */
  373.  
  374.     if (stems[stemno].vertical)
  375.     {
  376.         verticalondevice = !rotated;
  377.         stemstart = stems[stemno].x;
  378.         stemwidth = stems[stemno].dx;
  379.     }
  380.     else
  381.     {
  382.         verticalondevice = rotated;
  383.         stemstart = stems[stemno].y;
  384.         stemwidth = stems[stemno].dy;
  385.     }
  386.  
  387.     /* Determine how many pixels (non-negative) correspond to 1 character space
  388.      unit (unitpixels), and how many character space units (non-negative)
  389.      correspond to one pixel (onepixel). */
  390.  
  391.     if (stems[stemno].vertical)
  392.         p = ILoc(CharSpace, 1, 0);
  393.     else
  394.         p = ILoc(CharSpace, 0, 1);
  395.     QueryLoc(p, IDENTITY, &Xpixels, &Ypixels);
  396.     Destroy(p);
  397.     if (verticalondevice)
  398.         unitpixels = FABS(Xpixels);
  399.     else
  400.         unitpixels = FABS(Ypixels);
  401.  
  402.     onepixel = 1.0 / unitpixels;
  403.  
  404.     /**********************/
  405.     /* ADJUST STEM WIDTHS */
  406.     /**********************/
  407.  
  408.     widthdiff = 0.0;
  409.  
  410.     /* Find standard stem with smallest width difference from this stem */
  411.     if (stems[stemno].vertical)
  412.     {            /* vertical stem */
  413.         if (blues->StdVW != 0)    /* there is an entry for StdVW */
  414.             widthdiff = blues->StdVW - stemwidth;
  415.         for (i = 0; i < blues->numStemSnapV; ++i)
  416.         {        /* now look at StemSnapV */
  417.             if (blues->StemSnapV[i] - stemwidth < widthdiff)
  418.                 /* this standard width is the best match so far for this stem */
  419.                 widthdiff = blues->StemSnapV[i] - stemwidth;
  420.         }
  421.     }
  422.     else
  423.     {            /* horizontal stem */
  424.         if (blues->StdHW != 0)    /* there is an entry for StdHW */
  425.             widthdiff = blues->StdHW - stemwidth;
  426.         for (i = 0; i < blues->numStemSnapH; ++i)
  427.         {        /* now look at StemSnapH */
  428.             if (blues->StemSnapH[i] - stemwidth < widthdiff)
  429.                 /* this standard width is the best match so far for this stem */
  430.                 widthdiff = blues->StemSnapH[i] - stemwidth;
  431.         }
  432.     }
  433.  
  434.     /* Only expand or contract stems if they differ by less than 1 pixel from
  435.      the closest standard width, otherwise make the width difference = 0. */
  436.     if (FABS(widthdiff) > onepixel)
  437.         widthdiff = 0.0;
  438.  
  439.     /* Expand or contract stem to the nearest integral number of pixels. */
  440.     idealwidth = ROUND((stemwidth + widthdiff) * unitpixels);
  441.     /* Ensure that all stems are at least one pixel wide. */
  442.     if (idealwidth == 0)
  443.         idealwidth = 1;
  444.     /* Apply ForceBold to vertical stems. */
  445.     if (blues->ForceBold && stems[stemno].vertical)
  446.         /* Force this vertical stem to be at least DEFAULTBOLDSTEMWIDTH wide. */
  447.         if (idealwidth < DEFAULTBOLDSTEMWIDTH)
  448.             idealwidth = DEFAULTBOLDSTEMWIDTH;
  449.     /* Now compute the number of character space units necessary */
  450.     widthdiff = idealwidth * onepixel - stemwidth;
  451.  
  452.     /*********************************************************************/
  453.     /* ALIGNMENT ZONES AND OVERSHOOT SUPPRESSION - HORIZONTAL STEMS ONLY */
  454.     /*********************************************************************/
  455.  
  456.     stemshift = 0.0;
  457.  
  458.     if (!stems[stemno].vertical)
  459.     {
  460.  
  461.         /* Get bottom and top boundaries of the stem. */
  462.         stembottom = stemstart;
  463.         stemtop = stemstart + stemwidth;
  464.  
  465.         /* Find out if this stem intersects an alignment zone (the BlueFuzz  */
  466.         /* entry in the Private dictionary specifies the number of character */
  467.         /* units to extend (in both directions) the effect of an alignment   */
  468.         /* zone on a horizontal stem.  The default value of BlueFuzz is 1.   */
  469.         for (i = 0; i < numalignmentzones; ++i)
  470.         {
  471.             if (alignmentzones[i].topzone)
  472.             {
  473.                 if (stemtop >= alignmentzones[i].bottomy &&
  474.                     stemtop <= alignmentzones[i].topy + blues->BlueFuzz)
  475.                 {
  476.                     break;    /* We found a top-zone */
  477.                 }
  478.             }
  479.             else
  480.             {
  481.                 if (stembottom <= alignmentzones[i].topy &&
  482.                     stembottom >= alignmentzones[i].bottomy - blues->BlueFuzz)
  483.                 {
  484.                     break;    /* We found a bottom-zone */
  485.                 }
  486.             }
  487.         }
  488.  
  489.         if (i < numalignmentzones)
  490.         {        /* We found an intersecting zone (number i). */
  491.             suppressovershoot = FALSE;
  492.             enforceovershoot = FALSE;
  493.  
  494.             /* When 1 character space unit is rendered smaller than BlueScale
  495.          device units (pixels), we must SUPPRESS overshoots.  Otherwise,
  496.          if the top (or bottom) of this stem is more than BlueShift character
  497.          space units away from the flat position, we must ENFORCE overshoot. */
  498.  
  499.             if (unitpixels < blues->BlueScale)
  500.                 suppressovershoot = TRUE;
  501.             else if (alignmentzones[i].topzone)
  502.                 if (stemtop >= alignmentzones[i].bottomy + blues->BlueShift)
  503.                     enforceovershoot = TRUE;
  504.                 else if (stembottom <= alignmentzones[i].topy - blues->BlueShift)
  505.                     enforceovershoot = TRUE;
  506.  
  507.             /*************************************************/
  508.             /* ALIGN THE FLAT POSITION OF THE ALIGNMENT ZONE */
  509.             /*************************************************/
  510.  
  511.             /* Compute the position of the alignment zone's flat position in
  512.          device space and the amount of shift needed to align it on a
  513.          pixel boundary. Move all stems this amount. */
  514.  
  515.             if (alignmentzones[i].topzone)
  516.                 flatposition = alignmentzones[i].bottomy;
  517.             else
  518.                 flatposition = alignmentzones[i].topy;
  519.  
  520.             /* Find the flat position in pixels */
  521.             flatpospixels = flatposition * unitpixels;
  522.  
  523.             /* Find the stem shift necessary to align the flat
  524.          position on a pixel boundary, and use this shift for all stems */
  525.             stemshift = (ROUND(flatpospixels) - flatpospixels) * onepixel;
  526.  
  527.             /************************************************/
  528.             /* HANDLE OVERSHOOT ENFORCEMENT AND SUPPRESSION */
  529.             /************************************************/
  530.  
  531.             /* Compute overshoot amount (non-negative) */
  532.             if (alignmentzones[i].topzone)
  533.                 overshoot = stemtop - flatposition;
  534.             else
  535.                 overshoot = flatposition - stembottom;
  536.  
  537.             if (overshoot > 0.0)
  538.             {
  539.                 /* ENFORCE overshoot by shifting the entire stem (if necessary) so that
  540.            it falls at least one pixel beyond the flat position. */
  541.  
  542.                 if (enforceovershoot)
  543.                     if (overshoot < onepixel)
  544.                         if (alignmentzones[i].topzone)
  545.                             stemshift += onepixel - overshoot;
  546.                         else
  547.                             stemshift -= onepixel - overshoot;
  548.  
  549.                 /* SUPPRESS overshoot by aligning the stem to the alignment zone's
  550.            flat position. */
  551.  
  552.                 if (suppressovershoot)
  553.                     if (alignmentzones[i].topzone)
  554.                         stemshift -= overshoot;
  555.                     else
  556.                         stemshift += overshoot;
  557.             }
  558.  
  559.             /************************************************************/
  560.             /* COMPUTE HINT VALUES FOR EACH SIDE OF THE HORIZONTAL STEM */
  561.             /************************************************************/
  562.  
  563.             /* If the stem was aligned by a topzone, we expand or contract the stem
  564.          only at the bottom - since the stem top was aligned by the zone.
  565.          If the stem was aligned by a bottomzone, we expand or contract the stem
  566.          only at the top - since the stem bottom was aligned by the zone. */
  567.             if (alignmentzones[i].topzone)
  568.             {
  569.                 lbhintvalue = stemshift - widthdiff;    /* bottom */
  570.                 rthintvalue = stemshift;    /* top    */
  571.             }
  572.             else
  573.             {
  574.                 lbhintvalue = stemshift;    /* bottom */
  575.                 rthintvalue = stemshift + widthdiff;    /* top    */
  576.             }
  577.  
  578.             stems[stemno].lbhint = (struct segment *)Permanent(Loc(CharSpace, 0.0, lbhintvalue));
  579.             stems[stemno].lbrevhint = (struct segment *)Permanent(Loc(CharSpace, 0.0, -lbhintvalue));
  580.             stems[stemno].rthint = (struct segment *)Permanent(Loc(CharSpace, 0.0, rthintvalue));
  581.             stems[stemno].rtrevhint = (struct segment *)Permanent(Loc(CharSpace, 0.0, -rthintvalue));
  582.  
  583.             return;
  584.  
  585.         }        /* endif (i < numalignmentzones) */
  586.  
  587.         /* We didn't find any alignment zones intersecting this stem, so
  588.        proceed with normal stem alignment below. */
  589.  
  590.     }            /* endif (!stems[stemno].vertical) */
  591.  
  592.     /* Align stem with pixel boundaries on device */
  593.     stemstart = stemstart - widthdiff / 2;
  594.     stemshift = ROUND(stemstart * unitpixels) * onepixel - stemstart;
  595.  
  596.     /* Adjust the boundaries of the stem */
  597.     lbhintvalue = stemshift - widthdiff / 2;    /* left  or bottom */
  598.     rthintvalue = stemshift + widthdiff / 2;    /* right or top    */
  599.  
  600.     if (stems[stemno].vertical)
  601.     {
  602.         stems[stemno].lbhint = (struct segment *)Permanent(Loc(CharSpace, lbhintvalue, 0.0));
  603.         stems[stemno].lbrevhint = (struct segment *)Permanent(Loc(CharSpace, -lbhintvalue, 0.0));
  604.         stems[stemno].rthint = (struct segment *)Permanent(Loc(CharSpace, rthintvalue, 0.0));
  605.         stems[stemno].rtrevhint = (struct segment *)Permanent(Loc(CharSpace, -rthintvalue, 0.0));
  606.     }
  607.     else
  608.     {
  609.         stems[stemno].lbhint = (struct segment *)Permanent(Loc(CharSpace, 0.0, lbhintvalue));
  610.         stems[stemno].lbrevhint = (struct segment *)Permanent(Loc(CharSpace, 0.0, -lbhintvalue));
  611.         stems[stemno].rthint = (struct segment *)Permanent(Loc(CharSpace, 0.0, rthintvalue));
  612.         stems[stemno].rtrevhint = (struct segment *)Permanent(Loc(CharSpace, 0.0, -rthintvalue));
  613.     }
  614. }
  615.  
  616. #define LEFT   1
  617. #define RIGHT  2
  618. #define BOTTOM 3
  619. #define TOP    4
  620.  
  621. /*********************************************************************/
  622. /* Adjust a point using the given stem hint.  Use the left/bottom    */
  623. /* hint value or the right/top hint value depending on where the     */
  624. /* point lies in the stem.                                           */
  625. /*********************************************************************/
  626. static struct segment *Applyhint(struct segment *p, int stemnumber, int half)
  627. {
  628.     if (half == LEFT || half == BOTTOM)
  629.         return Join(p, stems[stemnumber].lbhint);    /* left  or bottom hint */
  630.     else
  631.         return Join(p, stems[stemnumber].rthint);    /* right or top    hint */
  632. }
  633.  
  634.  
  635. /*********************************************************************/
  636. /* Adjust a point using the given reverse hint.  Use the left/bottom */
  637. /* hint value or the right/top hint value depending on where the     */
  638. /* point lies in the stem.                                           */
  639. /*********************************************************************/
  640. static struct segment *Applyrevhint(struct segment *p, int stemnumber, int half)
  641. {
  642.     if (half == LEFT || half == BOTTOM)
  643.         return Join(p, stems[stemnumber].lbrevhint);    /* left  or bottom hint */
  644.     else
  645.         return Join(p, stems[stemnumber].rtrevhint);    /* right or top    hint */
  646. }
  647.  
  648.  
  649. /***********************************************************************/
  650. /* Find the vertical and horizontal stems that the current point       */
  651. /* (x, y) may be involved in.  At most one horizontal and one vertical */
  652. /* stem can apply to a single point, since there are no overlaps       */
  653. /* allowed.                                                            */
  654. /*   The actual hintvalue is returned as a location.                   */
  655. /* Hints are ignored inside a DotSection.                              */
  656. /***********************************************************************/
  657. static struct segment *FindStems(double x, double y, double dx, double dy)
  658. {
  659.     int i;
  660.     int newvert, newhor;
  661.     struct segment *p;
  662.     int newhorhalf, newverthalf;
  663.  
  664.     if (InDotSection)
  665.         return (NULL);
  666.  
  667.     newvert = newhor = -1;
  668.     newhorhalf = newverthalf = -1;
  669.  
  670.     for (i = currstartstem; i < numstems; i++)
  671.     {
  672.         if (stems[i].vertical)
  673.         {        /* VSTEM hint */
  674.             if ((x >= stems[i].x - EPS) &&
  675.                 (x <= stems[i].x + stems[i].dx + EPS))
  676.             {
  677.                 newvert = i;
  678.                 if (dy != 0.0)
  679.                 {
  680.                     if (dy < 0)
  681.                         newverthalf = LEFT;
  682.                     else
  683.                         newverthalf = RIGHT;
  684.                 }
  685.                 else
  686.                 {
  687.                     if (x < stems[i].x + stems[i].dx / 2)
  688.                         newverthalf = LEFT;
  689.                     else
  690.                         newverthalf = RIGHT;
  691.                 }
  692.             }
  693.         }
  694.         else
  695.         {        /* HSTEM hint */
  696.             if ((y >= stems[i].y - EPS) &&
  697.                 (y <= stems[i].y + stems[i].dy + EPS))
  698.             {
  699.                 newhor = i;
  700.                 if (dx != 0.0)
  701.                 {
  702.                     if (dx < 0)
  703.                         newhorhalf = TOP;
  704.                     else
  705.                         newhorhalf = BOTTOM;
  706.                 }
  707.                 else
  708.                 {
  709.                     if (y < stems[i].y + stems[i].dy / 2)
  710.                         newhorhalf = BOTTOM;
  711.                     else
  712.                         newhorhalf = TOP;
  713.                 }
  714.             }
  715.         }
  716.     }
  717.  
  718.     p = NULL;
  719.  
  720.     if (newvert == -1 && oldvert == -1) ;    /* Outside of any hints */
  721.     else if (newvert == oldvert &&
  722.          newverthalf == oldverthalf) ;    /* No hint change */
  723.     else if (oldvert == -1)
  724.     {            /* New vertical hint in effect */
  725.         p = Applyhint(p, newvert, newverthalf);
  726.     }
  727.     else if (newvert == -1)
  728.     {            /* Old vertical hint no longer in effect */
  729.         p = Applyrevhint(p, oldvert, oldverthalf);
  730.     }
  731.     else
  732.     {            /* New vertical hint in effect, old hint no longer in effect */
  733.         p = Applyrevhint(p, oldvert, oldverthalf);
  734.         p = Applyhint(p, newvert, newverthalf);
  735.     }
  736.  
  737.     if (newhor == -1 && oldhor == -1) ;    /* Outside of any hints */
  738.     else if (newhor == oldhor &&
  739.          newhorhalf == oldhorhalf) ;    /* No hint change */
  740.     else if (oldhor == -1)
  741.     {            /* New horizontal hint in effect */
  742.         p = Applyhint(p, newhor, newhorhalf);
  743.     }
  744.     else if (newhor == -1)
  745.     {            /* Old horizontal hint no longer in effect */
  746.         p = Applyrevhint(p, oldhor, oldhorhalf);
  747.     }
  748.     else
  749.     {            /* New horizontal hint in effect, old hint no longer in effect */
  750.         p = Applyrevhint(p, oldhor, oldhorhalf);
  751.         p = Applyhint(p, newhor, newhorhalf);
  752.     }
  753.  
  754.     oldvert = newvert;
  755.     oldverthalf = newverthalf;
  756.     oldhor = newhor;
  757.     oldhorhalf = newhorhalf;
  758.  
  759.     return p;
  760. }
  761.  
  762.  
  763. /******************************************************/
  764. /* Subroutines and statics for the Type1Char routines */
  765. /******************************************************/
  766.  
  767. static int strindex;        /* index into PostScript string being interpreted */
  768. static double currx, curry;    /* accumulated x and y values for hints */
  769.  
  770. struct callstackentry
  771. {
  772.     psobj *currstrP;    /* current CharStringP */
  773.     int currindex;        /* current strindex */
  774.     unsigned short currkey;    /* current decryption key */
  775. };
  776.  
  777. static double Stack[MAXSTACK];
  778. static int Top;
  779. static struct callstackentry CallStack[MAXCALLSTACK];
  780. static int CallTop;
  781. static double PSFakeStack[MAXPSFAKESTACK];
  782. static int PSFakeTop;
  783.  
  784. static void ClearStack(void)
  785. {
  786.     Top = -1;
  787. }
  788.  
  789. static void Push(double Num)
  790. {
  791.     if (++Top < MAXSTACK)
  792.         Stack[Top] = Num;
  793.     else
  794.         Error0("Push: Stack full\n");
  795. }
  796.  
  797. static void ClearCallStack(void)
  798. {
  799.     CallTop = -1;
  800. }
  801.  
  802. static void PushCall(psobj *CurrStrP, int CurrIndex, unsigned short CurrKey)
  803. {
  804.     if (++CallTop < MAXCALLSTACK)
  805.     {
  806.         CallStack[CallTop].currstrP = CurrStrP;    /* save CharString pointer */
  807.         CallStack[CallTop].currindex = CurrIndex;    /* save CharString index */
  808.         CallStack[CallTop].currkey = CurrKey;    /* save decryption key */
  809.     }
  810.     else
  811.         Error0("PushCall: Stack full\n");
  812. }
  813.  
  814. static void PopCall(psobj **CurrStrPP, int *CurrIndexP, unsigned short *CurrKeyP)
  815. {
  816.     if (CallTop >= 0)
  817.     {
  818.         *CurrStrPP = CallStack[CallTop].currstrP;    /* restore CharString pointer */
  819.         *CurrIndexP = CallStack[CallTop].currindex;    /* restore CharString index */
  820.         *CurrKeyP = CallStack[CallTop--].currkey;    /* restore decryption key */
  821.     }
  822.     else
  823.         Error0("PopCall: Stack empty\n");
  824. }
  825.  
  826. static void ClearPSFakeStack(void)
  827. {
  828.     PSFakeTop = -1;
  829. }
  830.  
  831. /* PSFakePush: Pushes a number onto the fake PostScript stack */
  832. static void PSFakePush(double Num)
  833. {
  834.     if (++PSFakeTop < MAXPSFAKESTACK)
  835.         PSFakeStack[PSFakeTop] = Num;
  836.     else
  837.         Error0("PSFakePush: Stack full\n");
  838. }
  839.  
  840. /* PSFakePop: Removes a number from the top of the fake PostScript stack */
  841. static double PSFakePop(void)
  842. {
  843.     if (PSFakeTop < 0)
  844.         Error0Ret("PSFakePop : Stack empty\n", 0.0);
  845.  
  846.     return (PSFakeStack[PSFakeTop--]);
  847. }
  848.  
  849. /***********************************************************************/
  850. /* Center a stem on the pixel grid -- used by HStem3 and VStem3        */
  851. /***********************************************************************/
  852. static struct segment *CenterStem(double edge1, double edge2)
  853. {
  854.     int idealwidth, verticalondevice;
  855.     double leftx, lefty, rightx, righty, center, width;
  856.     double widthx, widthy;
  857.     double shift, shiftx, shifty;
  858.     double Xpixels, Ypixels;
  859.     struct segment *p;
  860.  
  861.     p = Loc(CharSpace, edge1, 0.0);
  862.     QueryLoc(p, IDENTITY, &leftx, &lefty);
  863.  
  864.     p = Join(p, Loc(CharSpace, edge2, 0.0));
  865.     QueryLoc(p, IDENTITY, &rightx, &righty);
  866.     Destroy(p);
  867.  
  868.     widthx = FABS(rightx - leftx);
  869.     widthy = FABS(righty - lefty);
  870.  
  871.     if (widthy <= EPS)
  872.     {            /* verticalondevice hint */
  873.         verticalondevice = TRUE;
  874.         center = (rightx + leftx) / 2.0;
  875.         width = widthx;
  876.     }
  877.     else if (widthx <= EPS)
  878.     {            /* horizontal hint */
  879.         verticalondevice = FALSE;
  880.         center = (righty + lefty) / 2.0;
  881.         width = widthy;
  882.     }
  883.     else
  884.     {            /* neither horizontal nor verticalondevice and not oblique */
  885.         return (NULL);
  886.     }
  887.  
  888.     idealwidth = ROUND(width);
  889.     if (idealwidth == 0)
  890.         idealwidth = 1;
  891.     if (ODD(idealwidth))
  892.     {            /* is ideal width odd? */
  893.         /* center stem over pixel */
  894.         shift = FLOOR(center) + 0.5 - center;
  895.     }
  896.     else
  897.     {
  898.         /* align stem on pixel boundary */
  899.         shift = ROUND(center) - center;
  900.     }
  901.  
  902.     if (verticalondevice)
  903.     {
  904.         shiftx = shift;
  905.         shifty = 0.0;
  906.     }
  907.     else
  908.     {
  909.         shifty = shift;
  910.         shiftx = 0.0;
  911.     }
  912.  
  913.     p = Loc(IDENTITY, shiftx, shifty);
  914.     QueryLoc(p, CharSpace, &Xpixels, &Ypixels);
  915.     wsoffsetX = Xpixels;
  916.     wsoffsetY = Ypixels;
  917.     currx += wsoffsetX;
  918.     curry += wsoffsetY;
  919.  
  920.     return (p);
  921. }
  922.  
  923. /*-----------------------------------------------------------------------
  924.   Decrypt - From Adobe Type 1 book page 63, with some modifications
  925. -----------------------------------------------------------------------*/
  926. #define KEY 4330        /* Initial key (seed) for CharStrings decryption */
  927. #define C1 52845        /* Multiplier for pseudo-random number generator */
  928. #define C2 22719        /* Constant for pseudo-random number generator */
  929.  
  930. static unsigned short r;    /* Pseudo-random sequence of keys */
  931.  
  932. static unsigned char Decrypt(unsigned char cipher)
  933. {
  934.     unsigned char plain;
  935.  
  936.     plain = cipher ^ (r >> 8);
  937.     r = (cipher + r) * C1 + C2;
  938.     return plain;
  939. }
  940.  
  941. /* Get the next byte from the codestring being interpreted */
  942. static int DoRead(int *CodeP)
  943. {
  944.     if (strindex >= CharStringP->len)
  945.         return (FALSE);    /* end of string */
  946.     *CodeP = Decrypt((unsigned char)CharStringP->data.stringP[strindex++]);
  947.     return (TRUE);
  948. }
  949.  
  950. /* Strip blues->lenIV bytes from CharString and update encryption key */
  951. /* (the lenIV entry in the Private dictionary specifies the number of */
  952. /* random bytes at the beginning of each CharString; default is 4)    */
  953. static void StartDecrypt(void)
  954. {
  955.     int Code;
  956.  
  957.     r = KEY;        /* Initial key (seed) for CharStrings decryption */
  958.     for (strindex = 0; strindex < blues->lenIV;)
  959.         if (!DoRead(&Code))    /* Read a byte and update decryption key */
  960.             Error0("StartDecrypt: Premature end of CharString\n");
  961. }
  962.  
  963. static void Decode(int Code)
  964. {
  965.     int Code1, Code2, Code3, Code4;
  966.  
  967.     if (Code <= 31)        /* Code is [0,31]    */
  968.         DoCommand(Code);
  969.     else if (Code <= 246)    /* Code is [32,246]  */
  970.         Push((double)(Code - 139));
  971.     else if (Code <= 250)
  972.     {            /* Code is [247,250] */
  973.         if (!DoRead(&Code2))
  974.             goto ended;
  975.         Push((double)(((Code - 247) << 8) + Code2 + 108));
  976.     }
  977.     else if (Code <= 254)
  978.     {            /* Code is [251,254] */
  979.         if (!DoRead(&Code2))
  980.             goto ended;
  981.         Push((double)(-((Code - 251) << 8) - Code2 - 108));
  982.     }
  983.     else
  984.     {            /* Code is 255 */
  985.         if (!DoRead(&Code1))
  986.             goto ended;
  987.         if (!DoRead(&Code2))
  988.             goto ended;
  989.         if (!DoRead(&Code3))
  990.             goto ended;
  991.         if (!DoRead(&Code4))
  992.             goto ended;
  993.         Push((double)((((((Code1 << 8) + Code2) << 8) + Code3) << 8) + Code4));
  994.     }
  995.     return;
  996.  
  997.       ended:Error0("Decode: Premature end of Type 1 CharString");
  998. }
  999.  
  1000. /* Interpret a command code */
  1001. static void DoCommand(int Code)
  1002. {
  1003.     switch (Code)
  1004.     {
  1005.     case HSTEM:        /* |- y dy HSTEM |- */
  1006.         /* Vertical range of a horizontal stem zone */
  1007.         if (Top < 1)
  1008.             Error0("DoCommand: Stack low\n");
  1009.         HStem(Stack[0], Stack[1]);
  1010.         ClearStack();
  1011.         break;
  1012.     case VSTEM:        /* |- x dx VSTEM |- */
  1013.         /* Horizontal range of a vertical stem zone */
  1014.         if (Top < 1)
  1015.             Error0("DoCommand: Stack low\n");
  1016.         VStem(Stack[0], Stack[1]);
  1017.         ClearStack();
  1018.         break;
  1019.     case VMOVETO:        /* |- dy VMOVETO |- */
  1020.         /* Vertical MOVETO, equivalent to 0 dy RMOVETO */
  1021.         if (Top < 0)
  1022.             Error0("DoCommand: Stack low\n");
  1023.         RMoveTo(0.0, Stack[0]);
  1024.         ClearStack();
  1025.         break;
  1026.     case RLINETO:        /* |- dx dy RLINETO |- */
  1027.         /* Like RLINETO in PostScript */
  1028.         if (Top < 1)
  1029.             Error0("DoCommand: Stack low\n");
  1030.         RLineTo(Stack[0], Stack[1]);
  1031.         ClearStack();
  1032.         break;
  1033.     case HLINETO:        /* |- dx HLINETO |- */
  1034.         /* Horizontal LINETO, equivalent to dx 0 RLINETO */
  1035.         if (Top < 0)
  1036.             Error0("DoCommand: Stack low\n");
  1037.         RLineTo(Stack[0], 0.0);
  1038.         ClearStack();
  1039.         break;
  1040.     case VLINETO:        /* |- dy VLINETO |- */
  1041.         /* Vertical LINETO, equivalent to 0 dy RLINETO */
  1042.         if (Top < 0)
  1043.             Error0("DoCommand: Stack low\n");
  1044.         RLineTo(0.0, Stack[0]);
  1045.         ClearStack();
  1046.         break;
  1047.     case RRCURVETO:
  1048.         /* |- dx1 dy1 dx2 dy2 dx3 dy3 RRCURVETO |- */
  1049.         /* Relative RCURVETO, equivalent to dx1 dy1 */
  1050.         /* (dx1+dx2) (dy1+dy2) (dx1+dx2+dx3) */
  1051.         /* (dy1+dy2+dy3) RCURVETO in PostScript */
  1052.         if (Top < 5)
  1053.             Error0("DoCommand: Stack low\n");
  1054.         RRCurveTo(Stack[0], Stack[1], Stack[2], Stack[3],
  1055.               Stack[4], Stack[5]);
  1056.         ClearStack();
  1057.         break;
  1058.     case CLOSEPATH:    /* - CLOSEPATH |- */
  1059.         /* Closes a subpath without repositioning the */
  1060.         /* current point */
  1061.         DoClosePath();
  1062.         ClearStack();
  1063.         break;
  1064.     case CALLSUBR:        /* subr# CALLSUBR - */
  1065.         /* Calls a CharString subroutine with index */
  1066.         /* subr# from the Subrs array */
  1067.         if (Top < 0)
  1068.             Error0("DoCommand: Stack low\n");
  1069.         CallSubr((int)Stack[Top--]);
  1070.         break;
  1071.     case RETURN:        /* - RETURN - */
  1072.         /* Returns from a Subrs array CharString */
  1073.         /* subroutine called with CALLSUBR */
  1074.         Return();
  1075.         break;
  1076.     case ESCAPE:        /* ESCAPE to two-byte command code */
  1077.         if (!DoRead(&Code))
  1078.             Error0("DoCommand: ESCAPE is last byte\n");
  1079.         Escape(Code);
  1080.         break;
  1081.     case HSBW:        /* |- sbx wx HSBW |- */
  1082.         /* Set the left sidebearing point to (sbx,0), */
  1083.         /* set the character width vector to (wx,0). */
  1084.         /* Equivalent to sbx 0 wx 0 SBW.  Space */
  1085.         /* character should have sbx = 0 */
  1086.         if (Top < 1)
  1087.             Error0("DoCommand: Stack low\n");
  1088.         Sbw(Stack[0], 0.0, Stack[1], 0.0);
  1089.         ClearStack();
  1090.         break;
  1091.     case ENDCHAR:        /* - ENDCHAR |- */
  1092.         /* Finishes a CharString outline */
  1093.         EndChar();
  1094.         ClearStack();
  1095.         break;
  1096.     case RMOVETO:        /* |- dx dy RMOVETO |- */
  1097.         /* Behaves like RMOVETO in PostScript */
  1098.         if (Top < 1)
  1099.             Error0("DoCommand: Stack low\n");
  1100.         RMoveTo(Stack[0], Stack[1]);
  1101.         ClearStack();
  1102.         break;
  1103.     case HMOVETO:        /* |- dx HMOVETO |- */
  1104.         /* Horizontal MOVETO. Equivalent to dx 0 RMOVETO */
  1105.         if (Top < 0)
  1106.             Error0("DoCommand: Stack low\n");
  1107.         RMoveTo(Stack[0], 0.0);
  1108.         ClearStack();
  1109.         break;
  1110.     case VHCURVETO:    /* |- dy1 dx2 dy2 dx3 VHCURVETO |- */
  1111.         /* Vertical-Horizontal CURVETO, equivalent to */
  1112.         /* 0 dy1 dx2 dy2 dx3 0 RRCURVETO */
  1113.         if (Top < 3)
  1114.             Error0("DoCommand: Stack low\n");
  1115.         RRCurveTo(0.0, Stack[0], Stack[1], Stack[2],
  1116.               Stack[3], 0.0);
  1117.         ClearStack();
  1118.         break;
  1119.     case HVCURVETO:    /* |- dx1 dx2 dy2 dy3 HVCURVETO |- */
  1120.         /* Horizontal-Vertical CURVETO, equivalent to */
  1121.         /* dx1 0 dx2 dy2 0 dy3 RRCURVETO */
  1122.         if (Top < 3)
  1123.             Error0("DoCommand: Stack low\n");
  1124.         RRCurveTo(Stack[0], 0.0, Stack[1], Stack[2], 0.0, Stack[3]);
  1125.         ClearStack();
  1126.         break;
  1127.     default:        /* Unassigned command code */
  1128.         ClearStack();
  1129.         Error1("DoCommand: Unassigned code %d\n", Code);
  1130.     }
  1131. }
  1132.  
  1133. static void Escape(int Code)
  1134. {
  1135.     int i, Num;
  1136.     struct segment *p;
  1137.  
  1138.     switch (Code)
  1139.     {
  1140.     case DOTSECTION:    /* - DOTSECTION |- */
  1141.         /* Brackets an outline section for the dots in */
  1142.         /* letters such as "i", "j", and "!". */
  1143.         DotSection();
  1144.         ClearStack();
  1145.         break;
  1146.     case VSTEM3:        /* |- x0 dx0 x1 dx1 x2 dx2 VSTEM3 |- */
  1147.         /* Declares the horizontal ranges of three */
  1148.         /* vertical stem zones between x0 and x0+dx0, */
  1149.         /* x1 and x1+dx1, and x2 and x2+dx2. */
  1150.         if (Top < 5)
  1151.             Error0("DoCommand: Stack low\n");
  1152.         if (!wsset && ProcessHints)
  1153.         {
  1154.             /* Shift the whole character so that the middle stem is centered. */
  1155.             p = CenterStem(Stack[2] + sidebearingX, Stack[3]);
  1156.             path = Join(path, p);
  1157.             wsset = 1;
  1158.         }
  1159.  
  1160.         VStem(Stack[0], Stack[1]);
  1161.         VStem(Stack[2], Stack[3]);
  1162.         VStem(Stack[4], Stack[5]);
  1163.         ClearStack();
  1164.         break;
  1165.     case HSTEM3:        /* |- y0 dy0 y1 dy1 y2 dy2 HSTEM3 |- */
  1166.         /* Declares the vertical ranges of three hori- */
  1167.         /* zontal stem zones between y0 and y0+dy0, */
  1168.         /* y1 and y1+dy1, and y2 and y2+dy2. */
  1169.         if (Top < 5)
  1170.             Error0("DoCommand: Stack low\n");
  1171.         HStem(Stack[0], Stack[1]);
  1172.         HStem(Stack[2], Stack[3]);
  1173.         HStem(Stack[4], Stack[5]);
  1174.         ClearStack();
  1175.         break;
  1176.     case SEAC:        /* |- asb adx ady bchar achar SEAC |- */
  1177.         /* Standard Encoding Accented Character. */
  1178.         if (Top < 4)
  1179.             Error0("DoCommand: Stack low\n");
  1180.         Seac(Stack[0], Stack[1], Stack[2],
  1181.              (unsigned char)Stack[3],
  1182.              (unsigned char)Stack[4]);
  1183.         ClearStack();
  1184.         break;
  1185.     case SBW:        /* |- sbx sby wx wy SBW |- */
  1186.         /* Set the left sidebearing point to (sbx,sby), */
  1187.         /* set the character width vector to (wx,wy). */
  1188.         if (Top < 3)
  1189.             Error0("DoCommand: Stack low\n");
  1190.         Sbw(Stack[0], Stack[1], Stack[2], Stack[3]);
  1191.         ClearStack();
  1192.         break;
  1193.     case DIV:        /* num1 num2 DIV quotient */
  1194.         /* Behaves like DIV in the PostScript language */
  1195.         if (Top < 1)
  1196.             Error0("DoCommand: Stack low\n");
  1197.         Stack[Top - 1] = Div(Stack[Top - 1], Stack[Top]);
  1198.         Top--;
  1199.         break;
  1200.     case CALLOTHERSUBR:
  1201.         /* arg1 ... argn n othersubr# CALLOTHERSUBR - */
  1202.         /* Make calls on the PostScript interpreter */
  1203.         if (Top < 1)
  1204.             Error0("DoCommand: Stack low\n");
  1205.         Num = Stack[Top - 1];
  1206.         if (Top < Num + 1)
  1207.             Error0("DoCommand: Stack low\n");
  1208.         for (i = 0; i < Num; i++)
  1209.             PSFakePush(Stack[Top - i - 2]);
  1210.         Top -= Num + 2;
  1211.         CallOtherSubr((int)Stack[Top + Num + 2]);
  1212.         break;
  1213.     case POP:        /* - POP number */
  1214.         /* Removes a number from the top of the */
  1215.         /* PostScript interpreter stack and pushes it */
  1216.         /* onto the Type 1 BuildChar operand stack */
  1217.         Push(PSFakePop());
  1218.         break;
  1219.     case SETCURRENTPOINT:    /* |- x y SETCURRENTPOINT |- */
  1220.         /* Sets the current point to (x,y) in absolute */
  1221.         /* character space coordinates without per- */
  1222.         /* forming a CharString MOVETO command */
  1223.         if (Top < 1)
  1224.             Error0("DoCommand: Stack low\n");
  1225.         SetCurrentPoint(Stack[0], Stack[1]);
  1226.         ClearStack();
  1227.         break;
  1228.     default:        /* Unassigned escape code command */
  1229.         ClearStack();
  1230.         Error1("Escape: Unassigned code %d\n", Code);
  1231.     }
  1232. }
  1233.  
  1234. /* |- y dy HSTEM |- */
  1235. /* Declares the vertical range of a horizontal stem zone */
  1236. /* between coordinates y and y + dy */
  1237. /* y is relative to the left sidebearing point */
  1238. static void HStem(double y, double dy)
  1239. {
  1240.     IfTrace2((FontDebug), "Hstem %f %f\n", &y, &dy);
  1241.     if (ProcessHints)
  1242.     {
  1243.         if (numstems >= MAXSTEMS)
  1244.             Error0("HStem: Too many hints\n");
  1245.         if (dy < 0.0)
  1246.         {
  1247.             y += dy;
  1248.             dy = -dy;
  1249.         }
  1250.         stems[numstems].vertical = FALSE;
  1251.         stems[numstems].x = 0.0;
  1252.         stems[numstems].y = sidebearingY + y + wsoffsetY;
  1253.         stems[numstems].dx = 0.0;
  1254.         stems[numstems].dy = dy;
  1255.         ComputeStem(numstems);
  1256.         numstems++;
  1257.     }
  1258. }
  1259.  
  1260. /* |- x dx VSTEM |- */
  1261. /* Declares the horizontal range of a vertical stem zone */
  1262. /* between coordinates x and x + dx */
  1263. /* x is relative to the left sidebearing point */
  1264. static void VStem(double x, double dx)
  1265. {
  1266.     IfTrace2((FontDebug), "Vstem %f %f\n", &x, &dx);
  1267.     if (ProcessHints)
  1268.     {
  1269.         if (numstems >= MAXSTEMS)
  1270.             Error0("VStem: Too many hints\n");
  1271.         if (dx < 0.0)
  1272.         {
  1273.             x += dx;
  1274.             dx = -dx;
  1275.         }
  1276.         stems[numstems].vertical = TRUE;
  1277.         stems[numstems].x = sidebearingX + x + wsoffsetX;
  1278.         stems[numstems].y = 0.0;
  1279.         stems[numstems].dx = dx;
  1280.         stems[numstems].dy = 0.0;
  1281.         ComputeStem(numstems);
  1282.         numstems++;
  1283.     }
  1284. }
  1285.  
  1286. /* |- dx dy RLINETO |- */
  1287. /* Behaves like RLINETO in PostScript */
  1288. static void RLineTo(double dx, double dy)
  1289. {
  1290.     struct segment *B;
  1291.  
  1292.     IfTrace2((FontDebug), "RLineTo %f %f\n", &dx, &dy);
  1293.  
  1294.     B = Loc(CharSpace, dx, dy);
  1295.  
  1296.     if (ProcessHints)
  1297.     {
  1298.         currx += dx;
  1299.         curry += dy;
  1300.         /* B = Join(B, FindStems(currx, curry)); */
  1301.         B = Join(B, FindStems(currx, curry, dx, dy));
  1302.     }
  1303.  
  1304.     path = Join(path, Line(B));
  1305. }
  1306.  
  1307. /* |- dx1 dy1 dx2 dy2 dx3 dy3 RRCURVETO |- */
  1308. /* Relative RCURVETO, equivalent to dx1 dy1 */
  1309. /* (dx1+dx2) (dy1+dy2) (dx1+dx2+dx3) */
  1310. /* (dy1+dy2+dy3) RCURVETO in PostScript */
  1311. static void RRCurveTo(double dx1, double dy1, double dx2, double dy2, double dx3, double dy3)
  1312. {
  1313.     struct segment *B, *C, *D;
  1314.  
  1315.     IfTrace4((FontDebug), "RRCurveTo %f %f %f %f ", &dx1, &dy1, &dx2, &dy2);
  1316.     IfTrace2((FontDebug), "%f %f\n", &dx3, &dy3);
  1317.  
  1318.     B = Loc(CharSpace, dx1, dy1);
  1319.     C = Loc(CharSpace, dx2, dy2);
  1320.     D = Loc(CharSpace, dx3, dy3);
  1321.  
  1322.     if (ProcessHints)
  1323.     {
  1324.         /* For a Bezier curve, we apply the full hint value to
  1325.        the Bezier C point (and thereby D point). */
  1326.         currx += dx1 + dx2 + dx3;
  1327.         curry += dy1 + dy2 + dy3;
  1328.         /* C = Join(C, FindStems(currx, curry)); */
  1329.         C = Join(C, FindStems(currx, curry, dx3, dy3));
  1330.     }
  1331.  
  1332.     /* Since XIMAGER is not completely relative, */
  1333.     /* we need to add up the delta values */
  1334.  
  1335.     C = Join(C, Dup(B));
  1336.     D = Join(D, Dup(C));
  1337.  
  1338.     path = Join(path, Bezier(B, C, D));
  1339. }
  1340.  
  1341. /* - CLOSEPATH |- */
  1342. /* Closes a subpath WITHOUT repositioning the */
  1343. /* current point */
  1344. static void DoClosePath(void)
  1345. {
  1346.     struct segment *CurrentPoint;
  1347.  
  1348.     IfTrace0((FontDebug), "DoClosePath\n");
  1349.     CurrentPoint = Phantom(path);
  1350.     path = ClosePath(path);
  1351.     path = Join(Snap(path), CurrentPoint);
  1352. }
  1353.  
  1354. /* subr# CALLSUBR - */
  1355. /* Calls a CharString subroutine with index */
  1356. /* subr# from the Subrs array */
  1357. static void CallSubr(int subrno)
  1358. {
  1359.     IfTrace1((FontDebug), "CallSubr %d\n", subrno);
  1360.     if ((subrno < 0) || (subrno >= SubrsP->len))
  1361.         Error0("CallSubr: subrno out of range\n");
  1362.     PushCall(CharStringP, strindex, r);
  1363.     CharStringP = &SubrsP->data.arrayP[subrno];
  1364.     StartDecrypt();
  1365. }
  1366.  
  1367. /* - RETURN - */
  1368. /* Returns from a Subrs array CharString */
  1369. /* subroutine called with CALLSUBR */
  1370. static void Return(void)
  1371. {
  1372.     IfTrace0((FontDebug), "Return\n");
  1373.     PopCall(&CharStringP, &strindex, &r);
  1374. }
  1375.  
  1376. /* - ENDCHAR |- */
  1377. /* Finishes a CharString outline */
  1378. /* Executes SETCHACHEDEVICE using a bounding box */
  1379. /* it computes directly from the character outline */
  1380. /* and using the width information acquired from a previous */
  1381. /* HSBW or SBW.  It then calls a special version of FILL */
  1382. /* or STROKE depending on the value of PaintType in the */
  1383. /* font dictionary */
  1384. static void EndChar(void)
  1385. {
  1386.     IfTrace0((FontDebug), "EndChar\n");
  1387.  
  1388.     /* There is no need to compute and set bounding box for
  1389.      the cache, since XIMAGER does that on the fly. */
  1390.  
  1391.     /* Perform a Closepath just in case the command was left out */
  1392.     path = ClosePath(path);
  1393.  
  1394.     /* Set character width */
  1395.     path = Join(Snap(path), Loc(CharSpace, escapementX, escapementY));
  1396.  
  1397. }
  1398.  
  1399. /* |- dx dy RMOVETO |- */
  1400. /* Behaves like RMOVETO in PostScript */
  1401. static void RMoveTo(double dx, double dy)
  1402. {
  1403.     struct segment *B;
  1404.  
  1405.     IfTrace2((FontDebug), "RMoveTo %f %f\n", &dx, &dy);
  1406.  
  1407.     B = Loc(CharSpace, dx, dy);
  1408.  
  1409.     if (ProcessHints)
  1410.     {
  1411.         currx += dx;
  1412.         curry += dy;
  1413.         /* B = Join(B, FindStems(currx, curry)); */
  1414.         B = Join(B, FindStems(currx, curry, 0.0, 0.0));
  1415.     }
  1416.  
  1417.     path = Join(path, B);
  1418. }
  1419.  
  1420. /* - DOTSECTION |- */
  1421. /* Brackets an outline section for the dots in */
  1422. /* letters such as "i", "j", and "!". */
  1423. static void DotSection(void)
  1424. {
  1425.     IfTrace0((FontDebug), "DotSection\n");
  1426.     InDotSection = !InDotSection;
  1427. }
  1428.  
  1429. /* |- asb adx ady bchar achar SEAC |- */
  1430. /* Standard Encoding Accented Character. */
  1431. static void Seac(double asb, double adx, double ady, unsigned char bchar, unsigned char achar)
  1432. {
  1433.     int Code;
  1434.     struct segment *mypath;
  1435.  
  1436.     IfTrace4((FontDebug), "SEAC %f %f %f %d ", &asb, &adx, &ady, bchar);
  1437.     IfTrace1((FontDebug), "%d\n", achar);
  1438.  
  1439.     /* Move adx - asb, ady over and up from base char's sbpoint. */
  1440.     /* (We use adx - asb to counteract the accents sb shift.) */
  1441.     /* The variables accentoffsetX/Y modify sidebearingX/Y in Sbw(). */
  1442.     /* Note that these incorporate the base character's sidebearing shift by */
  1443.     /* using the current sidebearingX, Y values. */
  1444.     accentoffsetX = sidebearingX + adx - asb;
  1445.     accentoffsetY = sidebearingY + ady;
  1446.  
  1447.     /* Set path = NULL to avoid complaints from Sbw(). */
  1448.     path = NULL;
  1449.  
  1450.     /* Go find the CharString for the accent's code via an upcall */
  1451.     CharStringP = GetType1CharString(Environment, achar);
  1452.     StartDecrypt();
  1453.  
  1454.     ClearStack();
  1455.     ClearPSFakeStack();
  1456.     ClearCallStack();
  1457.  
  1458.     for (;;)
  1459.     {
  1460.         if (!DoRead(&Code))
  1461.             break;
  1462.         Decode(Code);
  1463.         if (errflag)
  1464.             return;
  1465.     }
  1466.     /* Copy snapped path to mypath and set path to NULL as above. */
  1467.     mypath = (struct segment *) Snap(path);
  1468.     path = NULL;
  1469.  
  1470.     /* We must reset these to null now. */
  1471.     accentoffsetX = accentoffsetY = 0;
  1472.  
  1473.     /* go find the CharString for the base char's code via an upcall */
  1474.     CharStringP = GetType1CharString(Environment, bchar);
  1475.     StartDecrypt();
  1476.  
  1477.     ClearStack();
  1478.     ClearPSFakeStack();
  1479.     ClearCallStack();
  1480.  
  1481.     FinitStems();
  1482.     InitStems();
  1483.  
  1484.     for (;;)
  1485.     {
  1486.         if (!DoRead(&Code))
  1487.             break;
  1488.         Decode(Code);
  1489.         if (errflag)
  1490.             return;
  1491.     }
  1492.     path = Join(mypath, path);
  1493. }
  1494.  
  1495.  
  1496. /* |- sbx sby wx wy SBW |- */
  1497. /* Set the left sidebearing point to (sbx,sby), */
  1498. /* set the character width vector to (wx,wy). */
  1499. static void Sbw(double sbx, double sby, double wx, double wy)
  1500. {
  1501.     IfTrace4((FontDebug), "SBW %f %f %f %f\n", &sbx, &sby, &wx, &wy);
  1502.  
  1503.     escapementX = wx;    /* Character width vector */
  1504.     escapementY = wy;
  1505.  
  1506.     /* Sidebearing values are sbx, sby args, plus accent offset from Seac(). */
  1507.     sidebearingX = sbx + accentoffsetX;
  1508.     sidebearingY = sby + accentoffsetY;
  1509.  
  1510.     path = Join(path, Loc(CharSpace, sidebearingX, sidebearingY));
  1511.     if (ProcessHints)
  1512.     {
  1513.         currx = sidebearingX;
  1514.         curry = sidebearingY;
  1515.     }
  1516. }
  1517.  
  1518.  /* num1 num2 DIV quotient */
  1519. /* Behaves like DIV in the PostScript language */
  1520. static double Div(double num1, double num2)
  1521. {
  1522.     IfTrace2((FontDebug), "Div %f %f\n", &num1, &num2);
  1523.     return (num1 / num2);
  1524. }
  1525.  
  1526. /*
  1527.   The following four subroutines (FlxProc, FlxProc1, FlxProc2, and
  1528.   HintReplace) are C versions of the OtherSubrs Programs, which were
  1529.   were published in the Adobe Type 1 Font Format book.
  1530.  
  1531.   The Flex outline fragment is described by
  1532.     c1: (x0, y0) = c3: (x0, yshrink(y0)) or (xshrink(x0), y0)
  1533.      "  (x1, y1) =  "  (x1, yshrink(y1)) or (xshrink(x1), y1)
  1534.      "  (x2, y2) - reference point
  1535.     c2: (x0, y0) = c4: (x0, yshrink(y0)) or (xshrink(x0), y0)
  1536.      "  (x1, y1) =  "  (x1, yshrink(y1)) or (xshrink(x1), y1)
  1537.      "  (x2, y2) =  "  (x2, y2), rightmost endpoint
  1538.     c3: (x0, y0) - control point, 1st Bezier curve
  1539.      "  (x1, y1) - control point,      -"-
  1540.      "  (x2, y2) - end point,          -"-
  1541.     c4: (x0, y0) - control point, 2nd Bezier curve
  1542.      "  (x1, y1) - control point,      -"-
  1543.      "  (x2, y2) - end point,          -"-
  1544.     ep: (epY, epX) - final endpoint (should be same as c4: (x2, y2))
  1545.     idmin - minimum Flex height (1/100 pixel) at which to render curves
  1546. */
  1547.  
  1548. #define dtransform(dxusr,dyusr,dxdev,dydev) { \
  1549.   register struct segment *point = Loc(CharSpace, dxusr, dyusr); \
  1550.   QueryLoc(point, IDENTITY, dxdev, dydev); \
  1551.   Destroy(point); \
  1552. }
  1553.  
  1554. #define itransform(xdev,ydev,xusr,yusr) { \
  1555.   register struct segment *point = Loc(IDENTITY, xdev, ydev); \
  1556.   QueryLoc(point, CharSpace, xusr, yusr); \
  1557.   Destroy(point); \
  1558. }
  1559.  
  1560. #define transform(xusr,yusr,xdev,ydev) dtransform(xusr,yusr,xdev,ydev)
  1561.  
  1562. #define PaintType (0)
  1563.  
  1564. #define lineto(x,y) { \
  1565.   struct segment *CurrentPoint; \
  1566.   double CurrentX, CurrentY; \
  1567.   CurrentPoint = Phantom(path); \
  1568.   QueryLoc(CurrentPoint, CharSpace, &CurrentX, &CurrentY); \
  1569.   Destroy(CurrentPoint); \
  1570.   RLineTo(x - CurrentX, y - CurrentY); \
  1571. }
  1572.  
  1573. #define curveto(x0,y0,x1,y1,x2,y2) { \
  1574.   struct segment *CurrentPoint; \
  1575.   double CurrentX, CurrentY; \
  1576.   CurrentPoint = Phantom(path); \
  1577.   QueryLoc(CurrentPoint, CharSpace, &CurrentX, &CurrentY); \
  1578.   Destroy(CurrentPoint); \
  1579.   RRCurveTo(x0 - CurrentX, y0 - CurrentY, x1 - x0, y1 - y0, x2 - x1, y2 - y1); \
  1580. }
  1581.  
  1582. #define xshrink(x) ((x - c4x2) * shrink +c4x2)
  1583. #define yshrink(y) ((y - c4y2) * shrink +c4y2)
  1584.  
  1585. #define PickCoords(flag) \
  1586.   if (flag) { /* Pick "shrunk" coordinates */ \
  1587.     x0 = c1x0; y0 = c1y0; \
  1588.     x1 = c1x1; y1 = c1y1; \
  1589.     x2 = c1x2; y2 = c1y2; \
  1590.     x3 = c2x0; y3 = c2y0; \
  1591.     x4 = c2x1; y4 = c2y1; \
  1592.     x5 = c2x2; y5 = c2y2; \
  1593.   } else { /* Pick original coordinates */ \
  1594.     x0 = c3x0; y0 = c3y0; \
  1595.     x1 = c3x1; y1 = c3y1; \
  1596.     x2 = c3x2; y2 = c3y2; \
  1597.     x3 = c4x0; y3 = c4y0; \
  1598.     x4 = c4x1; y4 = c4y1; \
  1599.     x5 = c4x2; y5 = c4y2; \
  1600.   }
  1601.  
  1602. /* Added by Amish 1/7/95 to get rid of a bunch of SAS complaints (uninitialied vars) */
  1603. #define PickOriginalCoords \
  1604.   { /* Pick original coordinates */ \
  1605.     x0 = c3x0; y0 = c3y0; \
  1606.     x1 = c3x1; y1 = c3y1; \
  1607.     x2 = c3x2; y2 = c3y2; \
  1608.     x3 = c4x0; y3 = c4y0; \
  1609.     x4 = c4x1; y4 = c4y1; \
  1610.     x5 = c4x2; y5 = c4y2; \
  1611.   }
  1612.  
  1613.  
  1614. /* FlxProc() = OtherSubrs[0]; Main part of Flex          */
  1615. /*   Calling sequence: 'idmin epX epY 3 0 callothersubr' */
  1616. /*   Computes Flex values, and renders the Flex path,    */
  1617. /*   and returns (leaves) ending coordinates on stack    */
  1618. static void FlxProc(
  1619.         double c1x2, double c1y2, double c3x0, double c3y0,
  1620.         double c3x1, double c3y1, double c3x2, double c3y2,
  1621.         double c4x0, double c4y0, double c4x1, double c4y1,
  1622.         double c4x2, double c4y2, double epY, double epX, int idmin)
  1623. {
  1624.     double dmin;
  1625.     double c1x0, c1y0, c1x1, c1y1;
  1626.     double c2x0, c2y0, c2x1, c2y1, c2x2, c2y2;
  1627.     char yflag;
  1628.     double x0, y0, x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;
  1629.     double cxx, cyx, cxy, cyy;    /* Transformation matrix */
  1630.     int flipXY;
  1631.     double x, y;
  1632.     double erosion = 1;    /* Device parameter */
  1633.     /* Erosion may have different value specified in 'internaldict' */
  1634.     double shrink;
  1635.     double dX, dY;
  1636.     char erode;
  1637.     double eShift;
  1638.     double cx, cy;
  1639.     double ex, ey;
  1640.  
  1641.     Destroy(path);
  1642.     path = FlxOldPath;    /* Restore previous path (stored in FlxProc1) */
  1643.  
  1644.     if (ProcessHints)
  1645.     {
  1646.         dmin = ABS(idmin) / 100.0;    /* Minimum Flex height in pixels */
  1647.  
  1648.         c2x2 = c4x2;
  1649.         c2y2 = c4y2;    /* Point c2 = c4 */
  1650.  
  1651.         yflag = FABS(c1y2 - c3y2) > FABS(c1x2 - c3x2);    /* Flex horizontal? */
  1652.  
  1653.         QuerySpace(CharSpace, &cxx, &cyx, &cxy, &cyy);    /* Transformation matrix */
  1654.  
  1655.         if (FABS(cxx) < 0.00001 || FABS(cyy) < 0.00001)
  1656.             flipXY = -1;    /* Char on side */
  1657.         else if (FABS(cyx) < 0.00001 || FABS(cxy) < 0.00001)
  1658.             flipXY = 1;    /* Char upright */
  1659.         else
  1660.             flipXY = 0;    /* Char at angle */
  1661.  
  1662.         if (yflag)
  1663.         {        /* Flex horizontal */
  1664.             if (flipXY == 0 || c3y2 == c4y2)
  1665.             {    /* Char at angle or Flex height = 0 */
  1666. //                PickCoords(FALSE);    /* Pick original control points */
  1667.                 PickOriginalCoords;
  1668.             }
  1669.             else
  1670.             {
  1671.                 shrink = FABS((c1y2 - c4y2) / (c3y2 - c4y2));    /* Slope */
  1672.  
  1673.                 c1x0 = c3x0;
  1674.                 c1y0 = yshrink(c3y0);
  1675.                 c1x1 = c3x1;
  1676.                 c1y1 = yshrink(c3y1);
  1677.                 c2x0 = c4x0;
  1678.                 c2y0 = yshrink(c4y0);
  1679.                 c2x1 = c4x1;
  1680.                 c2y1 = yshrink(c4y1);
  1681.  
  1682.                 dtransform(0.0, ROUND(c3y2 - c1y2), &x, &y);    /* Flex height in pixels */
  1683.                 dY = FABS((flipXY == 1) ? y : x);
  1684.                 PickCoords(dY < dmin);    /* If Flex small, pick 'shrunk' control points */
  1685.  
  1686.                 if (FABS(y2 - c1y2) > 0.001)
  1687.                 {    /* Flex 'non-zero'? */
  1688.                     transform(c1x2, c1y2, &x, &y);
  1689.  
  1690.                     if (flipXY == 1)
  1691.                     {
  1692.                         cx = x;
  1693.                         cy = y;
  1694.                     }
  1695.                     else
  1696.                     {
  1697.                         cx = y;
  1698.                         cy = x;
  1699.                     }
  1700.  
  1701.                     dtransform(0.0, ROUND(y2 - c1y2), &x, &y);
  1702.                     dY = (flipXY == 1) ? y : x;
  1703.                     if (ROUND(dY) != 0)
  1704.                         dY = ROUND(dY);
  1705.                     else
  1706.                         dY = (dY < 0) ? -1 : 1;
  1707.  
  1708.                     erode = PaintType != 2 && erosion >= 0.5;
  1709.                     if (erode)
  1710.                         cy -= 0.5;
  1711.                     ey = cy + dY;
  1712.                     ey = CEIL(ey) - ey + FLOOR(ey);
  1713.                     if (erode)
  1714.                         ey += 0.5;
  1715.  
  1716.                     if (flipXY == 1)
  1717.                     {
  1718.                         itransform(cx, ey, &x, &y);
  1719.                     }
  1720.                     else
  1721.                     {
  1722.                         itransform(ey, cx, &x, &y);
  1723.                     }
  1724.  
  1725.                     eShift = y - y2;
  1726.                     y1 += eShift;
  1727.                     y2 += eShift;
  1728.                     y3 += eShift;
  1729.                 }
  1730.             }
  1731.         }
  1732.         else
  1733.         {        /* Flex vertical */
  1734.             if (flipXY == 0 || c3x2 == c4x2)
  1735.             {    /* Char at angle or Flex height = 0 */
  1736. //                PickCoords(FALSE);    /* Pick original control points */
  1737.                 PickOriginalCoords;
  1738.             }
  1739.             else
  1740.             {
  1741.                 shrink = FABS((c1x2 - c4x2) / (c3x2 - c4x2));    /* Slope */
  1742.  
  1743.                 c1x0 = xshrink(c3x0);
  1744.                 c1y0 = c3y0;
  1745.                 c1x1 = xshrink(c3x1);
  1746.                 c1y1 = c3y1;
  1747.                 c2x0 = xshrink(c4x0);
  1748.                 c2y0 = c4y0;
  1749.                 c2x1 = xshrink(c4x1);
  1750.                 c2y1 = c4y1;
  1751.  
  1752.                 dtransform(ROUND(c3x2 - c1x2), 0.0, &x, &y);    /* Flex height in pixels */
  1753.                 dX = FABS((flipXY == -1) ? y : x);
  1754.                 PickCoords(dX < dmin);    /* If Flex small, pick 'shrunk' control points */
  1755.  
  1756.                 if (FABS(x2 - c1x2) > 0.001)
  1757.                 {
  1758.                     transform(c1x2, c1y2, &x, &y);
  1759.                     if (flipXY == -1)
  1760.                     {
  1761.                         cx = y;
  1762.                         cy = x;
  1763.                     }
  1764.                     else
  1765.                     {
  1766.                         cx = x;
  1767.                         cy = y;
  1768.                     }
  1769.  
  1770.                     dtransform(ROUND(x2 - c1x2), 0.0, &x, &y);
  1771.                     dX = (flipXY == -1) ? y : x;
  1772.                     if (ROUND(dX) != 0)
  1773.                         dX = ROUND(dX);
  1774.                     else
  1775.                         dX = (dX < 0) ? -1 : 1;
  1776.  
  1777.                     erode = PaintType != 2 && erosion >= 0.5;
  1778.                     if (erode)
  1779.                         cx -= 0.5;
  1780.                     ex = cx + dX;
  1781.                     ex = CEIL(ex) - ex + FLOOR(ex);
  1782.                     if (erode)
  1783.                         ex += 0.5;
  1784.  
  1785.                     if (flipXY == -1)
  1786.                     {
  1787.                         itransform(cy, ex, &x, &y);
  1788.                     }
  1789.                     else
  1790.                     {
  1791.                         itransform(ex, cy, &x, &y);
  1792.                     }
  1793.  
  1794.                     eShift = x - x2;
  1795.                     x1 += eShift;
  1796.                     x2 += eShift;
  1797.                     x3 += eShift;
  1798.                 }
  1799.             }
  1800.         }
  1801.  
  1802.         if (x2 == x5 || y2 == y5)
  1803.         {
  1804.             lineto(x5, y5);
  1805.         }
  1806.         else
  1807.         {
  1808.             curveto(x0, y0, x1, y1, x2, y2);
  1809.             curveto(x3, y3, x4, y4, x5, y5);
  1810.         }
  1811.     }
  1812.     else
  1813.     {            /* ProcessHints is off */
  1814. //        PickCoords(FALSE);    /* Pick original control points */
  1815.         PickOriginalCoords;
  1816.         curveto(x0, y0, x1, y1, x2, y2);
  1817.         curveto(x3, y3, x4, y4, x5, y5);
  1818.     }
  1819.  
  1820.     PSFakePush(epY);
  1821.     PSFakePush(epX);
  1822. }
  1823.  
  1824. /* FlxProc1() = OtherSubrs[1]; Part of Flex            */
  1825. /*   Calling sequence: '0 1 callothersubr'             */
  1826. /*   Saves and clears path, then restores currentpoint */
  1827. static void FlxProc1(void)
  1828. {
  1829.     struct segment *CurrentPoint;
  1830.  
  1831.     CurrentPoint = Phantom(path);
  1832.  
  1833.     FlxOldPath = path;
  1834.     path = CurrentPoint;
  1835. }
  1836.  
  1837. /* FlxProc2() = OtherSubrs[2]; Part of Flex */
  1838. /*   Calling sequence: '0 2 callothersubr'  */
  1839. /*   Returns currentpoint on stack          */
  1840. static void FlxProc2(void)
  1841. {
  1842.     struct segment *CurrentPoint;
  1843.     double CurrentX, CurrentY;
  1844.  
  1845.     CurrentPoint = Phantom(path);
  1846.     QueryLoc(CurrentPoint, CharSpace, &CurrentX, &CurrentY);
  1847.     Destroy(CurrentPoint);
  1848.  
  1849.     /* Push CurrentPoint on fake PostScript stack */
  1850.     PSFakePush(CurrentX);
  1851.     PSFakePush(CurrentY);
  1852. }
  1853.  
  1854. /* HintReplace() = OtherSubrs[3]; Hint Replacement            */
  1855. /*   Calling sequence: 'subr# 1 3 callothersubr pop callsubr' */
  1856. /*   Reinitializes stem hint structure                        */
  1857. static void HintReplace(void)
  1858. {
  1859.     /* Effectively retire the current stems, but keep them around for */
  1860.     /* revhint use in case we are in a stem when we replace hints. */
  1861.     currstartstem = numstems;
  1862.  
  1863.     /* 'subr#' is left on PostScript stack (for 'pop callsubr') */
  1864. }
  1865.  
  1866. /* arg1 ... argn n othersubr# CALLOTHERSUBR - */
  1867. /* Make calls on the PostScript interpreter (or call equivalent C code) */
  1868. /* NOTE: The n arguments have been pushed on the fake PostScript stack */
  1869. static void CallOtherSubr(int othersubrno)
  1870. {
  1871.     IfTrace1((FontDebug), "CallOtherSubr %d\n", othersubrno);
  1872.  
  1873.     switch (othersubrno)
  1874.     {
  1875.     case 0:        /* OtherSubrs[0]; Main part of Flex */
  1876.         if (PSFakeTop < 16)
  1877.             Error0("CallOtherSubr: PSFakeStack low");
  1878.         ClearPSFakeStack();
  1879.         FlxProc(
  1880.                    PSFakeStack[0], PSFakeStack[1], PSFakeStack[2], PSFakeStack[3],
  1881.                    PSFakeStack[4], PSFakeStack[5], PSFakeStack[6], PSFakeStack[7],
  1882.                    PSFakeStack[8], PSFakeStack[9], PSFakeStack[10], PSFakeStack[11],
  1883.                    PSFakeStack[12], PSFakeStack[13], PSFakeStack[14], PSFakeStack[15],
  1884.                    (int)PSFakeStack[16]
  1885.             );
  1886.         break;
  1887.     case 1:        /* OtherSubrs[1]; Part of Flex */
  1888.         FlxProc1();
  1889.         break;
  1890.     case 2:        /* OtherSubrs[2]; Part of Flex */
  1891.         FlxProc2();
  1892.         break;
  1893.     case 3:        /* OtherSubrs[3]; Hint Replacement */
  1894.         HintReplace();
  1895.         break;
  1896.     default:
  1897.         {        /* call OtherSubrs[4] or higher if PostScript is present */
  1898.         }
  1899.     }
  1900. }
  1901.  
  1902. /* |- x y SETCURRENTPOINT |- */
  1903. /* Sets the current point to (x,y) in absolute */
  1904. /* character space coordinates without per- */
  1905. /* forming a CharString MOVETO command */
  1906. static void SetCurrentPoint(double x, double y)
  1907. {
  1908.     IfTrace2((FontDebug), "SetCurrentPoint %f %f\n", &x, &y);
  1909.  
  1910.     currx = x;
  1911.     curry = y;
  1912. }
  1913.  
  1914. /* The Type1Char routine for use by PostScript. */
  1915. /************************************************/
  1916. struct xobject *Type1Char(
  1917.         psfont *env,
  1918.         struct XYspace *S,
  1919.         psobj *charstrP,
  1920.         psobj *subrsP,
  1921.         psobj *osubrsP,
  1922.         struct blues_struct *bluesP,    /* FontID's ptr to the blues struct */
  1923.         int *modeP)
  1924. {
  1925.     int Code;
  1926.  
  1927.     path = NULL;
  1928.     errflag = FALSE;
  1929.  
  1930.     /* Make parameters available to all Type1 routines */
  1931.     Environment = env;
  1932.     CharSpace = S;        /* used when creating path elements */
  1933.     CharStringP = charstrP;
  1934.     SubrsP = subrsP;
  1935.     OtherSubrsP = osubrsP;
  1936.     ModeP = modeP;
  1937.  
  1938.     blues = bluesP;
  1939.  
  1940.     /* compute the alignment zones */
  1941.     ComputeAlignmentZones();
  1942.  
  1943.     StartDecrypt();
  1944.  
  1945.     ClearStack();
  1946.     ClearPSFakeStack();
  1947.     ClearCallStack();
  1948.  
  1949.     InitStems();
  1950.  
  1951.     currx = curry = 0;
  1952.     escapementX = escapementY = 0;
  1953.     sidebearingX = sidebearingY = 0;
  1954.     accentoffsetX = accentoffsetY = 0;
  1955.     wsoffsetX = wsoffsetY = 0;    /* No shift to preserve whitspace. */
  1956.     wsset = 0;        /* wsoffsetX,Y haven't been set yet. */
  1957.  
  1958.     for (;;)
  1959.     {
  1960.         if (!DoRead(&Code))
  1961.             break;
  1962.         Decode(Code);
  1963.         if (errflag)
  1964.             break;
  1965.     }
  1966.  
  1967.     FinitStems();
  1968.  
  1969.  
  1970.     /* Clean up if an error has occurred */
  1971.     if (errflag)
  1972.     {
  1973.         if (path != NULL)
  1974.         {
  1975.             Destroy(path);    /* Reclaim storage */
  1976.             path = NULL;    /* Indicate that character could not be built */
  1977.         }
  1978.     }
  1979.  
  1980.     return ((struct xobject *)path);
  1981. }
  1982.